package com.demo.understandmvc.控制器增强.requestBodyAdvice;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import com.demo.understandmvc.返回值处理.responseEntity.MyData;

@ControllerAdvice
public class MyResponseBodyAdvice implements ResponseBodyAdvice<MyData> {

	public MyResponseBodyAdvice() {
		System.out.println("MyResponseBodyAdvice");
	}

	@Override
	public boolean supports(MethodParameter returnType, Class converterType) {
		// 只支持MyData增强类型
		return returnType.getParameterType() == MyData.class;
	}

	@Override
	public MyData beforeBodyWrite(MyData body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
		// 根据firstName与lastName设置fullName属性，把fullName返回到响应体中
		body.setFullName(body.getFirstName() + body.getLastName());
		return body;
	}


}
